package com.sky.mapper.user;

import com.sky.pojo.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Description AddressBookMapper
 * @Author lyh
 * @Date 2024-10-08
 */
@Mapper
public interface AddressBookMapper {
    /**
     * 根据用户id查询用户所有地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id=#{userId}")
    List<AddressBook> getListByUserId(Long userId);

    /**
     * 新增地址
     * @param addressBook
     */
    void addAddress(AddressBook addressBook);

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Select("select * from address_book where id=#{id}")
    AddressBook getById(Long id);

    /**
     * 修改地址
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 将当前用户的所有地址设置为普通地址（非默认地址）
     * @param addressBook
     */
    @Update("update address_book set is_default=#{isDefault} where user_id=#{userId}")
     void setDefaultByUserId(AddressBook addressBook);

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Update("update address_book set is_default=#{isDefault} where id=#{id}")
    void setDefaultById(AddressBook addressBook);
    @Delete("delete from address_book where id=#{id}")
    void deleteById(Long id);

    @Select("select * from address_book where user_id=#{userId} and is_default=1")
    List<AddressBook> getDefault(Long userId);
}
